home *** CD-ROM | disk | FTP | other *** search
/ PC-Blue - MS DOS Public Domain Library / PC-Blue MS-DOS Public Domain Library - NYACC.iso / vol128 / demo.doc < prev    next >
Encoding:
Text File  |  1986-12-15  |  56.7 KB  |  2,113 lines

  1.  
  2.  
  3.  
  4.          
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.                      A.D.A PROLOG Documentation Version 1.1
  31.                   for the Educational and Public Domain Versions
  32.  
  33.               Copyright Robert Morein and Automata Design Associates
  34.  
  35.                                  1570 Arran Way
  36.                                Dresher, Pa. 19025
  37.  
  38.                            Technical:  (215)-646-4894
  39.                            Orders:     (215)-355-5400
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.                                         1
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                                 Copyright Notice
  71.  
  72.  
  73.              Please be aware of the following:
  74.  
  75.         There are two A.D.A. PROLOG products distributed on this disk. 
  76.  
  77.              The  public domain PD PROLOG system has been contributed  to 
  78.         the  public domain for unrestricted use with one  exception:  the 
  79.         object  code  may not be  disassembled  or  modified.  Electronic 
  80.         bulletin  boards  and SIG groups are urged to aid in giving  this 
  81.         software the widest possible distribution.
  82.  
  83.              The second version is ED PROLOG,  an enhanced version of  PD 
  84.         PROLOG.  It is not in the public domain. If you have purchased ED 
  85.         PROLOG,  you  have a license to make as many backup copies as you 
  86.         wish.  Please  understand that unauthorized distribution of  this 
  87.         software  will  result  in even less food on the  table  here  at 
  88.         Automata Design Associates.
  89.  
  90.         This  documentation  may be reproduced freely,  but  may  not  be 
  91.         included  in any other document without the written permission of 
  92.         the author, which you probably will get. 
  93.  
  94.  
  95.          
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.                                         2
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.                                   Introduction
  138.                 
  139.  
  140.         We  hope  that you'll get some fun out of this  PROLOG.  It  will 
  141.         afford  you exposure to THE fifth generation language at the cost 
  142.         only  of  some  intellectual  effort.  The  motive  is  perfectly 
  143.         explicable:  We  want you to think of Automata Design  Associates 
  144.         for  fifth  generation  software.  It also gives us a  nice  warm 
  145.         feeling.
  146.              The  memory requirement is 128 k of transient program  area, 
  147.         plus  whatever  space is needed to execute programs  from  within 
  148.         PROLOG.  DOS  or  MSDOS 2.0 are required.  There are no  features 
  149.         requiring IBM PC architecture.
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.                                         3
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.                      Products by Automata Design Associates
  205.  
  206.              Automata  Design  Associates  specializes  in  software  for 
  207.         artificial  intelligence  and  robotic  applications.   A  PROLOG 
  208.         language  system is available in various configurations.  A  LISP  
  209.         interpreter will be introduced in March of 1985.
  210.  
  211.  
  212.              There  are five versions of PROLOG available  from  Automata 
  213.         Design  Associates.  All  of  them run under the MSDOS  or  PCDOS 
  214.         operating systems. Other environments will be supported soon.
  215.  
  216.  
  217.              .Public Domain PROLOG
  218.  
  219.         This  serves to further the general awareness of the public about 
  220.         PROLOG.  It  also is an excellent adjunct to anyone learning  the 
  221.         language.  Most  of  the core PROLOG described  by  Clocksin  and 
  222.         Mellish  in the book Programming In PROLOG is implemented.  Trace 
  223.         predicates  and  I/O  redirection  are  not.   
  224.  
  225.  
  226.              .Educational PROLOG
  227.  
  228.         At  extremely modest cost this affords an educational institution 
  229.         or  individual  a  PROLOG  system  which  provides  the   maximum 
  230.         available  programming  area  available  within  the  8086  small 
  231.         programming model.  Tracing,  a debugging aid,  allows monitoring 
  232.         a program as it runs.  User settable spy points selectively allow 
  233.         this.  Exhaustive  tracing  is also  available.  I/O  redirection 
  234.         gives some file ability.
  235.  
  236.              An  "exec"  function allows the execution of  a  program  or 
  237.         editor  from  within  PROLOG,  thus  encouraging  an  interactive 
  238.         environment.
  239.              
  240.              The cost of Educational PROLOG is $29.95.
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.                                         4
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.              .FSM PROLOG
  271.  
  272.              A  small  increment  in price adds full random  access  file 
  273.         capability. Character and structure I/O are allowed.
  274.              The  "asserta and "assertz" predicates are expanded and work 
  275.         with a clause indexing ability.  One can assert clauses  anywhere 
  276.         in the database under precise pattern matching control. 
  277.  
  278.              The cost of FSM PROLOG is $49.95
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.              .VMS PROLOG -- Virtual Memory
  286.  
  287.              At reasonable cost the addition of virtual memory gives  an 
  288.         expansion of capabilities of an order of magnitude. 
  289.  
  290.              The  database on disk is treated transparently.  No  special 
  291.         provisions  need  be  made  by the  user.  Virtual  and  resident 
  292.         databases  may be mixed.  A unique updating algorithim  preserves 
  293.         the format of the database as typed by the user while making only 
  294.         those changes necessary to make it equivalent to the database  in 
  295.         central memory.
  296.  
  297.              The cost of VMS PROLOG is $99.95
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.                                         5
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.              .VML PROLOG Large model Virtual Memory System
  337.  
  338.              
  339.  
  340.  
  341.  
  342.              A.D.A.  PROLOG is a remarkable fifth generation developement 
  343.         tool  for  the  implementation  of  intelligent  strategies   and 
  344.         optimized  control.  It  is both the kernel for  applications  of 
  345.         virtually  unlimited scope and a sophisticated developement  tool 
  346.         that multiplies the productivity of the programmer many times. 
  347.  
  348.              With  a  cost/performance ratio exceeding that of any  other 
  349.         product  and  a  compatibility  insured  by  compliance  to   the 
  350.         Edinburgh syntax, performance is enhanced by numerous extensions, 
  351.         many of them invisible to the user.
  352.  
  353.              A quick overview of some of the features discloses:
  354.  
  355.              1) Invisible compilation to a semantic network preserves the 
  356.              flexibility  of  the  interpreted mode and the  speed  of  a 
  357.              compiler.
  358.                   The programmer can compile and recompile any portion of 
  359.              a  module at any time.  The edit/compile/test cycle is short 
  360.              and free of strain. An interface is provided to an editor of 
  361.              choice.
  362.  
  363.              2) Floating point arithmetic with a full complement of input 
  364.              and output methods, transcendental and conversion functions.
  365.  
  366.              3)  Virtual memory.  Module size and number is unrestricted. 
  367.              Resident and virtual modules may be coresident.  Compilation 
  368.              is incremental. The cache algorithim is sophisticated.
  369.                   Changes  made in the database can be updated to disk by 
  370.              a single command.
  371.  
  372.              4) A powerful exec function,  and acceptance of stream input 
  373.              make integration into applications practical.
  374.  
  375.              5) Many   additional  built-in  predicates   enhance   the 
  376.              efficiency of the system.
  377.  
  378.              6) Debugging facilities let you see your program run without 
  379.              any additional generation steps.
  380.  
  381.              7)  Totally  invisible and incremental  garbage  collection. 
  382.              There is NEVER any wait for this function.
  383.  
  384.         The  cost  of this system is $300 for the MSDOS version.
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.                                         6
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.                                  Upgrade Policy
  401.  
  402.              Half  the  cost  of any A.D.A.  PROLOG  interpreter  may  be 
  403.         credited to the purchase of a higher level version.
  404.              The  full cost of VMS prolog may be applied to the  purchase 
  405.         of VMI or VML PROLOG.
  406.              Updates  to  a particular level product vary from $15.00  to 
  407.         $35.00.
  408.  
  409.                                 Run-time Packages
  410.  
  411.              Software  developers wishing to integrate an A.D.A.  product 
  412.         into  their  system  should inquire  about  specialized  run-time 
  413.         packages available at reasonable cost.
  414.  
  415.  
  416.                               To Place Your Order:
  417.  
  418.              The phone for orders is (215)-355-5400.  Visa, Mastercharge, 
  419.         and American Express are accepted.
  420.  
  421.  
  422.                               Technical Information
  423.  
  424.              Technical information may be obtained at (215) - 646- 4894
  425.  
  426.              Perhaps we can answer the following questions in advance:
  427.  
  428.              There is no support for:  APPLE II, Atari, Commodore, or  
  429.         CPM 80 .  Other machines from these manufactures may be supported 
  430.         in the future.
  431.  
  432.              The   public   domain   version  is   available   from   the 
  433.         organizations  who distribute such software.  It is not available 
  434.         from us.
  435.              The MSDOS products are available on 5" and 8" diskettes.
  436.              
  437.  
  438.                                      Returns
  439.  
  440.              The  software may be returned within 60 days of purchase for 
  441.         a full refund.  This applies whether or not the software has been 
  442.         used.  We do ask that manuals, disks and packaging be returned in 
  443.         excellent condition.
  444.  
  445.  
  446.  
  447.  
  448.                               To Place Your Order:
  449.  
  450.              The phone for orders is (215)-355-5400.  Visa, Mastercharge, 
  451.         and American Express are accepted.
  452.  
  453.  
  454.  
  455.  
  456.  
  457.                                         7
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                       How to run the Demonstration Programs 
  467.                         without Knowing What You're Doing
  468.  
  469.              We strongly advise that you purchase the book Programming in 
  470.         PROLOG by Clocksin and Mellish,  publisher Springer Verlag, 1981. 
  471.         For  the  impatient we give some advice.  Type the  demonstration 
  472.         program you wish to run.  There must be at least one entry  point 
  473.         within  the  program.  
  474.  
  475.         Note:  Please  understand that these are demonstrations programs. 
  476.         Regarding  user  interface,  they are poorly  written.  You  will 
  477.         probably have to read Clocksin and Mellish to appreciate that the 
  478.         following examples of input are not equivalent: "yes." , "yes" .
  479.  
  480.  
  481.         The animals program - "animal"
  482.          
  483.              Most  of the examples require C & M for  comprehension.  The 
  484.         program "animals", however, can be appreciated by anyone. It is a 
  485.         traditional   example of an expert system.
  486.              
  487.              We had hoped to include the animals program on disk,  but we 
  488.         have  found  to  our  dismay that the version which  we  used  is 
  489.         allegedly copyrighted by the implementors of PROLOG 86.  Don't be 
  490.         surprised - even "happy birthday" is copyrighted.  We will simply 
  491.         point out that the November '84 issue of Robotics Age included  a 
  492.         version  of  the  animals game,  which you can,  at the  risk  of 
  493.         copyright infringement,  type in.  There is only one change  that 
  494.         need  be  made.  The "concat" function used in that  program  has 
  495.         arguments of the form:
  496.  
  497.                       concat( [atom1, atom2,...], result ).
  498.  
  499.         In order to make the concat definition more closely resemble that 
  500.         of  "name",  which  is  described by Clocksin  and  Mellish,  the 
  501.         argments have been reversed:
  502.  
  503.                       concat( result, [atom1, atom2,...] )
  504.  
  505.         Assuming  that you have typed in the program and made the  change 
  506.         just noted, the following steps are required to run it:
  507.              
  508.              Run the prolog.exe file.  The prompt "?-" will appear.  Type 
  509.         "consult( 'animals' ).<CR>".  Here <CR> indicates you are to type 
  510.         a  carriage  return.  The PROLOG system will load  "animals"  and 
  511.         compile  it into an internal form.  When the "?-" prompt  appears 
  512.         PROLOG is ready to run the "animals" guessing game. The object of 
  513.         the program is to deduce the animal you are thinking of. To start 
  514.         it  off  type  "help.<CR>".  PROLOG  will  respond  by  asking  a 
  515.         question. 
  516.              Because of the way the animals program is written,  you must 
  517.         respond in a rigid format.  You may type "yes<CR>",  "no<CR>", or 
  518.         "why<CR>". 
  519.              Eventually the program will terminate with either a guess as 
  520.         to what animal you are thinking of,  or a remark that the  animal 
  521.  
  522.  
  523.                                         8
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.         is  not within its domain of knowledge.  The program has learned, 
  533.         however.  You  may  run  the  program again to  see  what  effect 
  534.         additional knowledge has on the program's behavior. 
  535.  
  536.              The program fragment "console" shows how you may improve the 
  537.         console input routines of any of these programs.
  538.              
  539.  
  540.         The Hematology Diagnosis Program - "hemat"
  541.  
  542.              Although  the  logical structure is not as sophisticated  as 
  543.         that of "animals", it is interesting for several reasons:
  544.  
  545.              1)  The  program  evaluates numerical data to  arrive  at  a 
  546.         diagnosis.
  547.  
  548.              2) Although inaccurate, it demonstrates that useful question 
  549.         answering systems are not difficult to write in PROLOG.
  550.  
  551.              3)  There  are  some mistakes in  the  program,  which  only 
  552.         slightly impede its usefulness. 
  553.  
  554.              This  program  uses  structure  input.  Terminate  all  your 
  555.         answers with a period, as in "y.<CR>", or "no.<CR>".
  556.  
  557.              The starting point is "signs.<CR>".  PROLOG will prompt  you 
  558.         for  signs  of  anemia.  The  program attempts  to  diagnose  two 
  559.         varieties of a hemolytic anemia.
  560.              The program could use a good working over by a  hematologist 
  561.         and we would be delighted to collaborate.
  562.  
  563.  
  564.         Prime Number Generator - "sieve"
  565.  
  566.              This  program demonstrates that anything can be programed in 
  567.         PROLOG if one tries hard enough.  Asking the  question   
  568.           "primes( 50, L ).<CR>" causes a list of prime numbers less than 
  569.         50  to be printed out.  "Sieve" is heavily recursive and  quickly 
  570.         exhausts the stack space of the small model interpreters.
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.                                         9
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                                   Special Offer
  599.  
  600.              For  some  inexplicable reason,  demonstration programs  are 
  601.         hard to come by. We are too busy writing PROLOG fill this gap. We 
  602.         will  reward the contribution of "cute" sample programs with  the 
  603.         following:
  604.  
  605.              1) A free  copy of type VMS virtual memory PROLOG
  606.  
  607.              2) The sample program will be published as an intact file 
  608.              together with whatever comments or advertisments the author
  609.              may see fit to include, on our distribution disks. 
  610.  
  611.              3) Exceptional contributions may merit a copy of type VML 
  612.              large  model virtual memory PROLOG which now incorporates  a 
  613.              UNIX1 style tree structured domain system.
  614.  
  615.  
  616.  
  617.         ______
  618.              1. Trademark of AT & T.
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.                                        10
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                              Running the Interpreter
  665.  
  666.         COMMANDS: Give commands in lower case.
  667.  
  668.         TO RUN:
  669.              Invoke PROLOG.EXE. After the "?-" prompt appears,
  670.                type "consult( <filename><CR> )", where <filename> is the 
  671.                desired database.
  672.              To exit, type "exitsys.<CR>"
  673.  
  674.         TO ASK A QUESTION:
  675.              At the prompt, type "<expression>.<CR>", where 
  676.              <expression>  is  a  question as described by  Clocksin  and 
  677.              Mellish. Be sure to terminate the question with a period.
  678.              The question may be up to 500 characters long.
  679.  
  680.         TO INPUT A STRUCTURE AT THE KEYBOARD:
  681.              The structure may be up to 500 characters in length. Be sure 
  682.              to terminate with a period.
  683.  
  684.         TO ASK FOR ANOTHER SOLUTION:
  685.              If a solution has been provided, the PROLOG interpreter will 
  686.              ask  "More?  (Y/N):".  Only  if  a "y"  is  typed  will  the 
  687.              interpreter perform a search.
  688.  
  689.         TO ABORT A SEARCH:
  690.              Simply   type   the  escape  key.   The   interpreter   will      
  691.              respond  with  "Interrrupted.",  and return to  the  command      
  692.              prompt.
  693.  
  694.         TO LOAD ANOTHER DATABASE:
  695.              Type "consult(<filename>).<CR>"
  696.  
  697.         TO REMOVE A DATABASE:
  698.              Type "forget(<filename>).<CR>"
  699.  
  700.         TO EXIT TO THE OPERATING SYSTEM:
  701.              Type "exitsys.<CR>"
  702.  
  703.              The system is totally interactive; any commands the operator 
  704.         gives  are and must be valid program statements.  Statements must 
  705.         terminate with a period.      All commands which take a file name 
  706.         also  accept a path name.  Any name which is not a  valid  PROLOG 
  707.         atom (refer to C & M) must be enclosed in single quotes. Thus one 
  708.         could say
  709.  
  710.              consult( expert )
  711.  
  712.         but one would need single quotes with
  713.  
  714.              consult( 'b:\samples\subtype\expert' ).
  715.  
  716.  
  717.         To exit the system, type "exitsys.<CR>"
  718.  
  719.  
  720.  
  721.                                        11
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.         Atoms  may contain MSDOS pathnames if they are enclosed by single 
  731.         quotes, ie.,  '\b:\samples\animal' .
  732.  
  733.         You may consult more than one file at a time.  However, all names 
  734.         are public and name conflicts must be avoided. The order in which 
  735.         modules are loaded may,  in cases of poor program design,  affect 
  736.         the behavior.
  737.  
  738.                                A Reference of Note
  739.  
  740.  
  741.              With  minor  exceptions,  the syntax is a superset  of  that 
  742.         described  by  Clocksin and Mellish in the  book  Programming  in 
  743.         Prolog by W.F.  Clocksin and C.S.  Mellish, published by Springer 
  744.         Verlag in Berlin, Heidelberg, New York, and Tokyo. We shall refer 
  745.         to this book as "C & M".
  746.  
  747.  
  748.              There   are  very  few   syntactical   differences,   mostly 
  749.         unrecognized and/or minor.
  750.              When  an operator is declared using the "op" statement,  the 
  751.         operator must be enclosed in single quotes in the "op"  statement 
  752.         itself,  if  it would not otherwise be a legal Edinburgh functor. 
  753.         Subsequently,  however,  the parser will recognize it for what it 
  754.         is,  except  in  the "unop" statement,  where it  must  again  be 
  755.         enclosed in single quotes.
  756.  
  757.              Variable numbers of functor paramaters are supported.
  758.  
  759.              A  goal  may be represented by a  variable,  which  is  less 
  760.         restrictive  than  the  C  &  M requirement  that  all  goals  be 
  761.         functors.  The  variable must be instantiated to a  functor  when 
  762.         that goal is pursued.
  763.  
  764.              Rules which appear inside other expressions must be enclosed 
  765.         in  parenthesis  if  the "," operator is to be  recognized  as  a 
  766.         logical connective.
  767.  
  768.              All  infix  operators described by C & M,  and user  defined 
  769.         infix,  prefix, and postfix operators with variable associativity 
  770.         and precedence are supported exactly as in C & M.
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.                                        12
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.                          The Built In Predicate Library
  797.  
  798.  
  799.              The  following built in predicates described by Clocksin and 
  800.         Mellish   are  supported.   Those  marked  with  asterisks   have 
  801.         additional features:
  802.              
  803.              arg
  804.              arithmetic operators:  "+", "-", "*", "/", <, <=, >, >=, mod
  805.             *asserta
  806.             *assertz
  807.              atom
  808.              atomic
  809.              call
  810.             *clause
  811.              clearops
  812.              consult
  813.              !
  814.             *debugging
  815.             *display
  816.              fail
  817.              functor
  818.             *get0
  819.              integer
  820.              is
  821.              list extraction operator: "|"
  822.              listing
  823.              logical connectives:  ",", ";",
  824.              mod
  825.              name
  826.              nl
  827.              nodebug
  828.              nonvar
  829.              not
  830.              notrace
  831.              op
  832.             *put
  833.             *read
  834.             *reconsult
  835.              repeat
  836.              retract
  837.              see
  838.              seeing
  839.              seen
  840.              spy
  841.              tab
  842.              tell
  843.              telling
  844.              told
  845.              trace
  846.              true
  847.              var
  848.  
  849.  
  850.  
  851.  
  852.  
  853.                                        13
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.                         Description of the Modifications.
  865.              
  866.  
  867.         call( <goal> )
  868.  
  869.              The  predicate as defined in C & M is obsolete.  The purpose 
  870.         was  to permit a goal search where the goal name was  a  variable 
  871.         instantiated  to  some functor name.  A.D.A.  permits writing  of 
  872.         goals with such names,  so the mediation of the "call" clause  is 
  873.         no longer necessary.
  874.  
  875.              The  "call"  predicate  may  be  trivially  implemented  for 
  876.         compatibility with the PROLOG definition
  877.  
  878.                                  call( X ) :- X.
  879.  
  880.  
  881.  
  882.         clause
  883.  
  884.              The  function  clause(  X,  Y ) has the  new  optional  form 
  885.         clause(  X,  Y,  I  ).  If the third variable is written,  it  is 
  886.         instantiated  to the current address of a clause in  memory.  The 
  887.         only  use of the result is with succeeding assertfa and  assertfz 
  888.         statements.
  889.  
  890.  
  891.  
  892.         cut
  893.              If  the cut is the last clause of a search the user will not 
  894.         be queried "More? Y/N" for additional solutions.
  895.  
  896.  
  897.  
  898.         debugging
  899.  
  900.              "Debugging"  prints a list of the current  spypoints.  After 
  901.         each name a sequence of numbers may appear, indicating the number 
  902.         of  arguments that is a condition of the trace.  The  word  "all" 
  903.         appears  if  the  number of arguments is not a condition  of  the 
  904.         trace.
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.                                        14
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.         op( <prec>, <assoc>, <functor> )
  929.  
  930.              Defines  the  user  definable  grammar  of  a  functor.  The 
  931.         definition conforms to that in C & M. We mention here a minor but 
  932.         important point.  If <functor> is not a valid PROLOG atom it must 
  933.         be   enclosed  in  single  quotes  when  declared  in  the   "op" 
  934.         declaration and when undeclared in the "unop" declaration.  It is 
  935.         not  necessary  or legal to do this when the functor is  actually 
  936.         being used as an operator.
  937.  
  938.         Output predicates
  939.  
  940.            display
  941.            write
  942.            print
  943.            put
  944.  
  945.              These  functions  have  been  modified  to  accept  multiple 
  946.         arguments in the form:
  947.  
  948.                        print( <arg1>, <arg2>, <arg3>,... )
  949.  
  950.         Thus, "put( a, b, c )" would result in the display of "abc".
  951.              The  names  of  some  PROLOG atoms that may  occur  are  not 
  952.         accepted  by  the  PROLOG scanner  unless  surrounded  by  single 
  953.         quotes.  This only applies when such an atom is read in, not when 
  954.         it is internally generated. Nevertheless, this presents us with a 
  955.         problem:  We  would  like to be capable of writing  valid  PROLOG 
  956.         terms to a file. In some cases, it is necessary to add the single 
  957.         quotes.  In other cases,  such as human oriented output, they are 
  958.         an irritant. The modified definitions of the following predicates 
  959.         are an attempt at a solution:
  960.  
  961.              display
  962.              Operator  precedence  is ignored,  all functors are  printed 
  963.              prefix and single quotes are printed if needed or they  were 
  964.              supplied if and when the atom was originally input.
  965.  
  966.              write
  967.              Operator  precedence is taken into account and operators are 
  968.              printed according to precedence.  Single quotes are  printed 
  969.              under the same conditions as for "display."
  970.  
  971.              print
  972.              Operator  precedence is taken into account and operators are 
  973.              printed  according to precedence.  Single quotes  are  never 
  974.              displayed.  This  is  a  human oriented form of  output  and 
  975.              should  never  be used for writing of terms for  the  PROLOG 
  976.              scanner.
  977.  
  978.              
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.                                        15
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.         get0
  995.         read
  996.  
  997.              The  functions  "get0"  and "read"  have  been  extended  to 
  998.         support input from a stream other than the one currently selected 
  999.         by "see". To direct output to a file or other stream, an optional 
  1000.         argument  is used.  For example,  "get0( char,  <file name> )" or 
  1001.         "get0( char,  user )" would cause input to come from <file  name> 
  1002.         or the console.  If the file has not already been opened,  "get0" 
  1003.         will fail.
  1004.  
  1005.  
  1006.  
  1007.              Atoms enclosed by single quotest, eg. '\nthis is a new line' 
  1008.         can contain the escape sequences
  1009.  
  1010.              '\n', '\r', '\t' and '\''.
  1011.  
  1012.              If these atoms are printed by "display" or "write" they  are 
  1013.         printed  just  as they are.  If they are printed by  the  "print" 
  1014.         clause they are translated as follows:
  1015.  
  1016.         '\n'  results  in  the printing of a carriage return and  a  line 
  1017.         feed.
  1018.         '\r' results in the printing of a carriage return only.
  1019.         '\t' results in the printing of a tab character.
  1020.         '\'' allows the printing of a single quote within a quoted atom.
  1021.  
  1022.              The "portray" feature is not presently implemented.
  1023.          
  1024.                         Description of the New Predicates
  1025.  
  1026.  
  1027.         clearops-
  1028.              Nullify  the  operator  status  of  every  operator  in  the 
  1029.              database.
  1030.  
  1031.         concat( (<variable> | <functor>), <List> )
  1032.              A  list  of functors or operators is concatenated  into  one 
  1033.         string,  which becomes the name of a new atom to which <variable> 
  1034.         or <functor> must match or be instantiated.
  1035.  
  1036.  
  1037.         dir( option )
  1038.              Provide  an  alphabetized listing to the console  of  atoms, 
  1039.         constants,   or   open  files.   Without  options,   simply  type 
  1040.         "dir.<CR>". Options are:
  1041.  
  1042.              dir( pred ) - list clause names only.
  1043.              dir( files ) - list open files only.
  1044.  
  1045.  
  1046.         exitsys
  1047.              Exit to the operating system.
  1048.  
  1049.  
  1050.  
  1051.                                        16
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.         forget( <file name> )
  1061.              Make  a database unavailable for use and reclaim the storage 
  1062.         it occupied.
  1063.  
  1064.  
  1065.         ratom( <arg>, <stream> )-
  1066.              Read an atom from the input stream,  to which <arg>  matches 
  1067.         or  is  instantiated.  <stream> is optional.  If <stream> is  not 
  1068.         given, the input stream defaults to the standar input.
  1069.              Input is terminated by a CR or LF, which are not included in 
  1070.         the stream.
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.                                        17
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.                              Arithmetic Capabilities
  1127.  
  1128.              Integer  arithmetic is supported.  Numbers are 32 bit signed 
  1129.         quantities. The following arithmetic operators are supported:
  1130.  
  1131.                       "+", "-", "*", "/", <, <=, >, >=, mod.
  1132.  
  1133.         Arithmetic operators must never be used as goals,  although  they 
  1134.         may be part of structures. It is legal to write:
  1135.  
  1136.                                     X = a + b
  1137.              
  1138.         which  results in the instantiation of X to the struture (a + b). 
  1139.         But the following is not legal:
  1140.  
  1141.                        alpha( X, Y ) :- X + Y, beta( Y ).
  1142.  
  1143.  
  1144.         Evaluation  of  an arithemtic expression is mediated by the  "is" 
  1145.         and inequality predicates.  For instance,  the following would be 
  1146.         correct:
  1147.  
  1148.                          alpha( X, Y, Z ) :- Z is X + Y.
  1149.  
  1150.                          beta( X, Y ) :- X + 2 < Y + 3.
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.                                        18
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.                                  Prolog Tutorial          
  1193.  
  1194.  
  1195.                                   Introduction
  1196.  
  1197.  
  1198.  
  1199.  
  1200.              Probably  you  have heard of the language PROLOG within  the 
  1201.         last year or so. You probably wondered the following things:
  1202.  
  1203.         1) What does the name stand for? Names of computer languages are 
  1204.         almost always acronyms.
  1205.  
  1206.         2) What is it good for?
  1207.  
  1208.         3) Why now?
  1209.  
  1210.         4) Can I get a copy to play with?
  1211.  
  1212.              Congratulations! You obviously know the answer to the fourth 
  1213.         question. We now respond to the other three.
  1214.              
  1215.         1)  The  name  stands for "programming in logic." This  we  shall 
  1216.         elaborate on in depth later on.
  1217.  
  1218.         2) PROLOG is good for writing question answering systems.  It  is 
  1219.         also   good   for  writing  programs  that  perform   complicated 
  1220.         strategies  that  compute the best or worst way to  accomplish  a 
  1221.         task, or avoid an undesirable result.
  1222.  
  1223.         3) PROLOG was virtually unknown in this country until researchers 
  1224.         in  Japan announced that it was to be the core language  of  that 
  1225.         country's fifth generation computer project.  This is the project 
  1226.         with  which  Japan hopes to achieve a domainant position  in  the 
  1227.         world information industry of the 1990's. 
  1228.  
  1229.              PROLOG  is  one of the most unusual computer languages  ever 
  1230.         invented.  It  cannot be compared to  FORTRAN,  PASCAL,  "C",  or 
  1231.         BASIC.  The facilities complement,  rather than replace those  of 
  1232.         conventional  languages.  Although  it  has great  potential  for 
  1233.         database  work,  it  has  nothing in  common  with  the  database 
  1234.         languages used on microcomputers.
  1235.  
  1236.              Perhaps  the  best point to make is that while  conventional 
  1237.         languages are prescriptive, PROLOG is descriptive. A statement in 
  1238.         a conventional language might read:
  1239.  
  1240.              if( car_wheels = TRUE ) then
  1241.                begin
  1242.                  (some sort of procedure)
  1243.                  X = X + 1;
  1244.                end 
  1245.  
  1246.  
  1247.  
  1248.  
  1249.                                        19
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.         A statment in PROLOG could just be a statment of fact about  cars 
  1259.         and wheels. There are many relationships that hold. For instance,
  1260.  
  1261.              has( car, wheels ).
  1262.  
  1263.              has( car, quant(wheels, four) ).
  1264.  
  1265.              round( wheels ).
  1266.  
  1267.         Each  of  these statments is an independent fact  relating  cars, 
  1268.         wheels,  and  the  characteristics of wheels.  Because  they  are 
  1269.         independent, they can be put into a PROLOG program by programmers 
  1270.         working separately. The man who is a specialist on car bodies can 
  1271.         say  his thing,  the wheel specialist can have his say,  and  the 
  1272.         participants can work with relative independence. And this brings 
  1273.         to light a major advantage of PROLOG:
  1274.  
  1275.  
  1276.                              PARALLEL PROGRAMMING!!!
  1277.                             
  1278.  
  1279.         With  conventional  programming languages projects can  still  be 
  1280.         "chunked",  or  divided between programmers.  But efficiency on a 
  1281.         team  project  drops  drastically below that  of  the  individual 
  1282.         programmer  wrapped  up  in  his own trance.  As  the  number  of 
  1283.         participants    grows   the   need   for   communication    grows 
  1284.         geometrically. The time spent communicating can exceed that spent 
  1285.         programming! 
  1286.              Although   PROLOG   does   not  eliminate   the   need   for 
  1287.         task  coordination,  the problem is considerably  simplified.  It 
  1288.         also provides the ability to answer questions in a "ready to  eat 
  1289.         form."  Consider your favorite BASIC interpreter.  Based upon the 
  1290.         statements about cars and wheels previously given,  could you ask 
  1291.         it the following question?   
  1292.  
  1293.                        
  1294.               has( car, X ), round( X ).
  1295.  
  1296.              Does  a  car  have anything which  is  round?  The  question 
  1297.         instructs the PROLOG interpreter to consider all the objects that 
  1298.         it  knows are possessed by a car and find those which are  round. 
  1299.         Perhaps  you are beginning to guess that PROLOG has the abilities 
  1300.         of a smart database searcher.  It can not only find the facts but 
  1301.         selectively find them and interpret them.
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.                                        20
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.              Consider the problem of a fault tree, as exemplified by this 
  1325.         abbreviated one:
  1326.  
  1327.  
  1328.  
  1329.         {Car won't start}
  1330.              | 
  1331.              | 
  1332.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  1333.                 (Yes)                                       v
  1334.                  |                                  {Check battery}
  1335.                  |
  1336.         [Smell gasoline](yes) --> {Try full throttle cranking}
  1337.                  |                       (failure)
  1338.         /--------/                           |
  1339.  
  1340.                             (details omitted)
  1341.  
  1342.  
  1343.  
  1344.              The fault tree is easily programmed in BASIC. Later we shall 
  1345.         show  that  PROLOG supplies a superior replacement for the  fault 
  1346.         tree.  Though the tree is capable of diagnosing only the  problem 
  1347.         for  which  it was designed,  PROLOG dynamically  constructs  the 
  1348.         appropriate  tree from facts and rules you have provided.  PROLOG 
  1349.         is not limited to answering one specific question.  Given  enough 
  1350.         information,  it  will attempt to find all deductive solutions to 
  1351.         any problem.
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.                                        21
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.                                   PROLOG PRIMER
  1392.  
  1393.         I.                       Rules and Facts     
  1394.  
  1395.  
  1396.  
  1397.              This  is  where you should start if you know  nothing  about 
  1398.         PROLOG. Let us consider a simple statment in PROLOG, such as:
  1399.  
  1400.         1)   has( car, wheels ).
  1401.  
  1402.         This  statement  is a "fact.  The word "has" in this statment  is 
  1403.         known  either  as a functor or predicate.  It is a name  for  the 
  1404.         relationship  within the parenthesis.  It implies that a car  has 
  1405.         wheels.  But  the  order  of  the words  inside  the  bracket  is 
  1406.         arbitrary and established by you. You could just as easily say:
  1407.  
  1408.         2)   has( wheels, car ).
  1409.  
  1410.         and if you wrote this way consistently,  all would be  well.  The 
  1411.         words  has,  wheels,  and car are all PROLOG atoms.  "Wheels" and 
  1412.         "car" are constants. 
  1413.              
  1414.         A   database   of  facts  can  illustrate  the   data   retrieval 
  1415.         capabilities of PROLOG. For instance:
  1416.  
  1417.         3)   has( car, wheels ).
  1418.              has( car, frame ).
  1419.              has( car, windshield ).
  1420.              has( car, engine ).
  1421.  
  1422.         You could then ask PROLOG the question:
  1423.  
  1424.         4)   has( car, Part ).
  1425.  
  1426.         The  capital  "P" of Part means that Part is a  variable.  PROLOG 
  1427.         will make Part equal to whatever constant is required to make the 
  1428.         question match one of the facts in the database. Thus PROLOG will 
  1429.         respond:
  1430.  
  1431.              Part = wheels.
  1432.              
  1433.              More?(Y/N):
  1434.  
  1435.         If you type "y" the next answer will appear:
  1436.  
  1437.              Part = frame.
  1438.  
  1439.              More?(Y/N):
  1440.  
  1441.         If you continue, PROLOG will produce the answers Part = windshield 
  1442.         and Part = engine. Finally, you will see:
  1443.  
  1444.              More?(Y/N):y
  1445.  
  1446.  
  1447.                                        22
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.              No.
  1458.          
  1459.         indicating that PROLOG has exhausted the database.  Incidentally, 
  1460.         when  a  variable is set equal to a constant or  other  variable, 
  1461.         it is said to be instantiated to that object.
  1462.  
  1463.              Notice  that  PROLOG searches the database forwards  and  in 
  1464.         this case,  from the beginning.  The forward search path is built 
  1465.         into PROLOG and cannot be changed. An author of a program written 
  1466.         in  a  prescriptive language is quite conscious of the  order  of 
  1467.         execution  of  his program,  while in PROLOG it is  not  directly 
  1468.         under his control.
  1469.              
  1470.              The other major element is the rule which is a fact which is 
  1471.         conditionally true. In logic this is called a Horn clause: 
  1472.  
  1473.  
  1474.         5)   has( X, wheels ) :- iscar( X ).
  1475.  
  1476.         The  fact iscar( car ) and the above rule are equivalent to
  1477.  
  1478.         6)   has( car, wheels).
  1479.  
  1480.         The  symbol :- is the "rule sign." The expression on the left  of 
  1481.         :-is the "head" and on the right is the body.  The variable X has 
  1482.         scope  of the rule,  which means that it has meaning only  within 
  1483.         the rule.  For instance,  we could have two rules in the database 
  1484.         using identically named variables.
  1485.  
  1486.  
  1487.         7)   has( X,  transportation ) :- 
  1488.                            has( X,  car ), has( license, X ).
  1489.  
  1490.         8)   has( X, elephant ) :- istrainer( X ), hasjob( X ).
  1491.  
  1492.         The  variables  X in the two expressions are completely  distinct 
  1493.         and have nothing to do with each other.
  1494.  
  1495.         The comma between has( X, car ) and has( license, X ) means "and" 
  1496.         or logical conjuction.  The rule will not be true unless both the 
  1497.         clauses has(X, car) and has( license, X ) are true.
  1498.  
  1499.  
  1500.              On the other hand if there is a rule
  1501.              
  1502.         9)   has( license, X ) :- passedexam( X ).
  1503.  
  1504.         consider what PROLOG will do in response to the question:
  1505.  
  1506.         10)  has( harry, transportation ).
  1507.  
  1508.         (Notice  that  harry has not been capitalized because we  do  not 
  1509.         want  it  taken as a variable.  We could,  however,  say  'Harry' 
  1510.         enclosed in single quotes.)
  1511.  
  1512.  
  1513.                                        23
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.  
  1523.              It  will scan the database and use (7),  in which X will  be 
  1524.         instantiated to harry. The rule generates two new questions:
  1525.  
  1526.         11)  has( harry, car ).
  1527.  
  1528.         12)  has( license, harry ).
  1529.  
  1530.         Assuming  that  harry  has  a car,  the first clause  of  (7)  is 
  1531.         satisfied and the database is scanned for a match to (12). PROLOG 
  1532.         picks  up  rule (9) in which X is instantiated to harry  and  the 
  1533.         question is now posed:
  1534.  
  1535.         13)  passedexam( harry ).
  1536.  
  1537.              If there is a fact:
  1538.  
  1539.              passedexam( harry ).
  1540.  
  1541.         in  the database then all is well and harry  has  transportation. 
  1542.         If there is not, then PROLOG will succinctly tell you:
  1543.  
  1544.              No.
  1545.  
  1546.         But  suppose Harry has money and can hire a chauffer as any  good 
  1547.         programmer  can.  That  could be made part of the program in  the 
  1548.         following way.
  1549.  
  1550.              The rule which PROLOG tried to use was:
  1551.  
  1552.         14)  has( X,  transportation ) :- 
  1553.                            has( X,  car ), has( license, X ).
  1554.  
  1555.         At any point following it there could be included another rule:
  1556.  
  1557.         15)  has( X, transportation ) :- has( X, money ).
  1558.  
  1559.         or simply the bald fact:
  1560.  
  1561.         16)  has( harry, transportation ).
  1562.  
  1563.              These  additional  rules  or  facts would  be  used  in  two 
  1564.         circumstances.  If at any point a rule does not yield a solution, 
  1565.         PROLOG   will  scan  forward  from  that  rule  to  find  another 
  1566.         applicable  one.  This process is known as "backtracking  search" 
  1567.         and can be quite time consuming.
  1568.  
  1569.  
  1570.         If  in response to the "More?" prompt you answer "y" PROLOG  will 
  1571.         search  for an additional distinct solution.  It will attempt  to 
  1572.         find an alternate rule or fact for the last rule or fact used. If 
  1573.         that  fails,  it  will back up to the antecedent rule and try  to 
  1574.         find  an alternate antecedent.  And it will continue to  back  up 
  1575.         until  it  arrives at the question you asked,  at which point  it 
  1576.         will say:
  1577.  
  1578.  
  1579.                                        24
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.  
  1589.              No.
  1590.  
  1591.         "Antecedent"  to a rule means that it gave rise to its' use.  For 
  1592.         example,  (7)  is  the antecedent of (9) in the  context  of  the 
  1593.         question (16).
  1594.  
  1595.  
  1596.  
  1597.  
  1598.         II.                          Grammar
  1599.  
  1600.              It is a boring subject, but it must be discussed. All PROLOG 
  1601.         statements are composed of valid terms, possibly a rule sign (":-
  1602.         "),  commas representing conjunction ("and"), and a period at the 
  1603.         very end.
  1604.              A term is a structure, constant, variable, or number.
  1605.          
  1606.              What is a structure? It is a kind of grouping:
  1607.  
  1608.              1) Structures consist of a functor, and a set of objects or
  1609.                 structures in parenthesis.
  1610.  
  1611.              2)  Objects are constants,  variables,  numbers,  or  lists, 
  1612.                 which we have not discussed yet.
  1613.  
  1614.              3)  A  constant or functor must be a string of  letters  and 
  1615.                 numbers, beginning with a lower case letter, unless
  1616.                 you  choose  to  enclose  it in single  quotes  (  'howdy 
  1617.                 pardner'  ),  in  which  case you are  freed  from  these 
  1618.                 restrictions.
  1619.              4) A  variable  must be a string of  letters  and  numbers 
  1620.                 beginning with a capital letter.
  1621.              
  1622.              5) A  functor  may optionally have  arguments  enclosed  in 
  1623.                 parenthesis , as in: hascar( X ) or hascar. 
  1624.  
  1625.         An example:  "has( X, transportation )." is a structure.
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.                                        25
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.         III.                     Input / Output      
  1655.  
  1656.              You   now   know  enough  to  write  simple  databases   and 
  1657.         interrogate   them  profitably.   But  before  we  examine   more 
  1658.         sophisticated  examples,  it  will be necessary to add input  and 
  1659.         output to the language. There are built in functions which appear 
  1660.         as rules which are satisfied once. Thus the statment:
  1661.  
  1662.              write( 'Hello world.' ).
  1663.  
  1664.         can be included on the right side of a rule:
  1665.  
  1666.  
  1667.         greetings(  X ) :- ishuman( X ),  write( 'Hello world.' ).  You 
  1668.         can  also write "write( X )" where X is some variable.  Note that 
  1669.         'Hello  world.' is not enclosed in double quotes.  Single quotes, 
  1670.         which denote a constant, are required. Double quotes would denote 
  1671.         a list, which is another thing entirely.
  1672.  
  1673.         Provided  that  a match to "ishuman" can be  found,  the  builtin 
  1674.         function  "write"  is  executed and the message  printed  to  the 
  1675.         screen.
  1676.              The  builtin  read( X ) reads a "structure" that  you  input 
  1677.         from the keyboard. More formally, we have
  1678.  
  1679.              read( <variable> or <constant> )
  1680.              write( <variable> or <constant> )
  1681.  
  1682.         If you write read( Input ),  then the variable "keyboard" will be 
  1683.         assigned to whatever is typed at the keyboard,  provided that the 
  1684.         input  is a valid PROLOG structure.  The builtin "read" will fail 
  1685.         if instead of Keyboard we wrote read( baloney ),  where "baloney" 
  1686.         is a constant,  and the user at the keyboard did not type exactly 
  1687.         "baloney." 
  1688.  
  1689.         When you input a structure in response to a "read" statement,  be 
  1690.         sure to end it with a period and an <ENTER>. 
  1691.  
  1692.              There  is  a convenient way of putting the cursor on  a  new 
  1693.         line. This is the builtin "nl". For example:
  1694.  
  1695.              showme :- write( 'line 1' ), nl, write( 'line 2' ). 
  1696.  
  1697.         would result in:
  1698.  
  1699.              line 1
  1700.              line 2
  1701.  
  1702.              There  is  also a primitive form of input/output for  single 
  1703.         characters. It will be discussed later.
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.                                        26
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.         IV.                   A Fault Tree Example
  1721.  
  1722.              Consider the "won't start" fault tree for an automobile:
  1723.  
  1724.         {Car won't start}
  1725.              | 
  1726.              | 
  1727.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  1728.                 (Yes)                                       v
  1729.                  |                                  {Check battery}
  1730.                  |
  1731.         [Smell gasoline](yes) --> {Try full throttle cranking}
  1732.                  |                       (failure)
  1733.         /--------/                           |
  1734.         |           /------------------------/ 
  1735.         |           |                       
  1736.         |           |
  1737.         |  [Check for fuel line leaks](yes)-->{Replace fuel line}
  1738.         |          (no)
  1739.         |           |
  1740.         |           |
  1741.         |  [Check for defective carburator](yes)--\
  1742.         |          (no)                           v
  1743.         |                                {Repair carburator}
  1744.         \----\
  1745.              |
  1746.              |
  1747.         [Is spark present](no)-->[Do points open and close](no)-\
  1748.              |                             (yes)                v
  1749.         /----/                               |          {Adjust points}
  1750.         |           /------------------------/
  1751.         |           |
  1752.         |  [Pull distributor wire, observe spark](blue)--\
  1753.         |        (orange)                                v
  1754.         |           |                           {Check plug wires & cap}
  1755.         |           |
  1756.         |  [Measure voltage on coil primary](not 12V)--\
  1757.         |         (12V)                                v
  1758.         |           |              {Check wiring, ballast resistor}
  1759.         |           |
  1760.         |  [Check condenser with ohmmeter](conducts)--\
  1761.         |    (no conduction)                          v
  1762.         |           |                         {Replace condenser}
  1763.         |           |
  1764.         |  [Open and close points](voltage not 0 - 12)--\
  1765.         |   (voltage swings 0 - 12)                     v
  1766.         |           |                         {Fix primary circuit}
  1767.         |           |
  1768.         |  {Consider hidden fault, swap components]
  1769.         |
  1770.         |
  1771.         \-------{Call a tow truck!!}
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.                                        27
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.              A PROLOG program to  implement this is simple. Each statment 
  1787.         represents  a  decision point fragment of the  tree.  The  PROLOG 
  1788.         interpreter  dynamically  assembles  the tree as  it  attempts  a 
  1789.         solution. 
  1790.  
  1791.         'car wont start' :- write( 'Is the battery voltage low?' ), 
  1792.                             affirm, nl,
  1793.                             write( 'Check battery' ).
  1794.  
  1795.         'car wont start' :- write( 'Smell gasoline?' ), 
  1796.                             affirm, nl,
  1797.                             'fuel system'.
  1798.  
  1799.         'fuel system'    :- write( 'Try full throttle cranking' ).
  1800.  
  1801.         'fuel system'    :- write( 'Are there fuel line leaks?' ),
  1802.                             affirm, nl,
  1803.                             write( 'Replace fuel line.' ).
  1804.  
  1805.         'fuel system'    :- write( 'Check carburator' ).
  1806.  
  1807.         'car wont start' :- write( 'Is spark present?' ),
  1808.                             not( affirm ), nl,
  1809.                             'no spark'.
  1810.  
  1811.         'no spark'       :- write( 'Do points open and close?' ),
  1812.                             not( affirm ), nl,
  1813.                             write( 'Adjust or replace points.' ).
  1814.  
  1815.         'no spark'       :- write( 'Is the spark off the coil good?' ),
  1816.                             affirm,
  1817.                             write( 'Check plug wires and cap.' ).
  1818.  
  1819.         'no spark'       :- write( 'What is the voltage on the primary
  1820.                              of the coil: ' ), 
  1821.                             read( Volts ), 
  1822.                             Volts < 10,
  1823.                             nl,
  1824.                             write('Check wiring and ballast resistor.').
  1825.  
  1826.         'no spark'       :- write( 'Does the capacitor leak?' ),
  1827.                             affirm,
  1828.                             write( 'Replace the capacitor.' ).
  1829.  
  1830.         'no spark'       :- not( 'primary circuit' ).
  1831.  
  1832.         'primary circuit' 
  1833.                          :- write( 'Open the  points.  Voltage  across 
  1834.                               coil?:'), nl,
  1835.                             read( Openvolts ), Openvolts < 1, 
  1836.                             write(  'Close the points.  Voltage  across 
  1837.                               coil?:'),
  1838.                             read( Closevolts ), Closevolts > 10, nl,
  1839.                             write( 'Primary circuit is OK.' ). 
  1840.  
  1841.  
  1842.  
  1843.                                        28
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.         'no spark'       :- write( 'Consider a hidden fault. Swap
  1853.                               cap, rotor,points,capacitor.' ).
  1854.  
  1855.  
  1856.         'Car wont start' :- write( 'Get a tow truck!!' ).
  1857.  
  1858.  
  1859.                                  --End program--
  1860.  
  1861.  
  1862.              The  above  is  a  simple example of  an  expert  system.  A 
  1863.         sophisticated  system would tell you exactly the method by  which 
  1864.         it  has reached a conclusion.  It would communicate by a  "shell" 
  1865.         program  written  in PROLOG which would accept a wider  range  of 
  1866.         input   than  the  "valid  structure"  required  by  the   PROLOG 
  1867.         interpreter directly.
  1868.  
  1869.  
  1870.  
  1871.  
  1872.  
  1873.  
  1874.  
  1875.  
  1876.  
  1877.  
  1878.  
  1879.  
  1880.  
  1881.  
  1882.  
  1883.  
  1884.  
  1885.  
  1886.  
  1887.  
  1888.  
  1889.  
  1890.  
  1891.  
  1892.  
  1893.  
  1894.  
  1895.  
  1896.  
  1897.  
  1898.  
  1899.  
  1900.  
  1901.  
  1902.  
  1903.  
  1904.  
  1905.  
  1906.  
  1907.  
  1908.  
  1909.                                        29
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.         V.                            Lists               
  1919.  
  1920.              Consider  a  shopping list given you by your wife.  It is  a 
  1921.         piece of paper with items written on it in an order that probably 
  1922.         symbolizes  their  importance.  At the top it  may  say  EGGS!!!, 
  1923.         followed by carrots, hamburger, and finally a flea collar for the 
  1924.         dog, if you can find one. In PROLOG such a list would be written:
  1925.  
  1926.         1)   [eggs, carrots, hamburger, fleacollar]
  1927.  
  1928.         The  order of a list is important so that eggs and carrots cannot 
  1929.         be reversed and PROLOG be uncaring.
  1930.  
  1931.         Let us put the list in a structure:
  1932.  
  1933.              shopping( [eggs, carrots, hamburger, fleacollar] ).
  1934.  
  1935.         Then  if you wished to isolate the head of the list you could ask 
  1936.         the question:
  1937.  
  1938.              shopping( [ Mostimportant | Rest ] ).
  1939.  
  1940.         and PROLOG would respond:
  1941.  
  1942.              Mostimportant   =  eggs,   
  1943.              Rest   =   [carrots,   hamburger, fleacollar].
  1944.  
  1945.         The vertical bar "|" is crucial here. It is the string extraction 
  1946.         operator,  which  performs  a  combination  of the  CDR  and  CAR 
  1947.         functions  of LISP.  When it appears in the context [X|Y] it  can 
  1948.         separate the head of the list from the rest, or tail.
  1949.  
  1950.  
  1951.              You  may have gained the impression that PROLOG is a  rather 
  1952.         static language capable of answering simple questions,  but it is 
  1953.         far  more powerful than that.  The string extraction operator  is 
  1954.         the  key.  It permits PROLOG to whittle a complex expression down 
  1955.         to the bare remainder.  If the rules you have given it permit  it 
  1956.         to  whittle  the  remainder  down to  nothing,  then  success  is 
  1957.         achieved. An example of this is the definition of "append."
  1958.  
  1959.              Let  us suppose you have not yet done yesterday's  shopping, 
  1960.         let alone today's. You pull it out of your wallet and sootch tape 
  1961.         it to the list your wife just gave you. Yesterday's list was:
  1962.  
  1963.              [tomatoes, onions, ketchup]
  1964.  
  1965.         Combined with [eggs, carrots, hamburger, fleacollar] we obtain
  1966.  
  1967.              [eggs,carrots,hamburger,fleacollar,tomatoes,onions,garlic].
  1968.  
  1969.         To  take one list and to attach it to the tail of another list is 
  1970.         to  "append"  the first to the second.  The PROLOG definition  of 
  1971.         append is:
  1972.  
  1973.  
  1974.  
  1975.                                        30
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.  
  1985.  
  1986.         Rule1:     append( [], L, L ).
  1987.  
  1988.         Rule2:     append( [X|List1], List2, [X|List3] ) :-
  1989.                       append( List1, List2, List3 ].
  1990.  
  1991.         The  general  scheme is this:  
  1992.  
  1993.         The definition consists of one rule and one fact.  The rule  will 
  1994.         be used over and over again until what little is left matches the 
  1995.         fact.  The [] stands for empty list,  which is like a bag without 
  1996.         anything in it. This is an example of a recursive definition.
  1997.              Suppose we ask:
  1998.  
  1999.              append( [a,b,c], [d,e,f], Whatgives ).
  2000.  
  2001.         1. Rule 2 is invoked with arguments ( [a,b,c], [d,e,f], Whatgives ).
  2002.         2. Rule 2 is invoked again with arguments:
  2003.              ( [b,c], [d,e,f], List3 ).
  2004.         3. Rule 2 is invoked again with arguments:
  2005.              ( [b], [d,e,f], List3 ).
  2006.         4.  The  arguments  are now ([],  [d,e,f],  List3 ).  Rule 1  now 
  2007.             matches. End.
  2008.  
  2009.         How does this cause a list to be constructed? The key is to watch 
  2010.         the   third  argument.   Supplied  by  the  user,   it  is  named 
  2011.         "Whatgives". The inference engine matches it to [X|List3] in rule 
  2012.         2. Now lets trace this as rule two is successivly invoked: 
  2013.  
  2014.  
  2015.                 Whatgives                                                
  2016.                    |                                                     
  2017.                    |                                                     
  2018.                    |                                                     
  2019.                    v                                                     
  2020.         Rule2:  [X|List3] (List3 = [b,c])                                
  2021.                  |  \                                                    
  2022.                  |   \                                                   
  2023.                  |    \                                                  
  2024.                  v     \                                                 
  2025.         Rule2:   a   [X'|List3'] (List3' = [c])                          
  2026.                       |    \                                             
  2027.                       |     \                                            
  2028.                       |      \                                           
  2029.                       v       \                                          
  2030.         Rule2:        b     [X''|List3''] (List3'' = [], ie., empty set.)
  2031.                               |    \                                     
  2032.                               |     \                                    
  2033.                               |      \                                   
  2034.         Rule1:                c       L  ( in Rule1 = [d,e,f] )              
  2035.                                                                          
  2036.         End.
  2037.  
  2038.  
  2039.  
  2040.  
  2041.                                        31
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.         L in rule 1 is [d,e,f] for the following reason: Notice that rule 
  2051.         2 never alters List2. It supplies it to whatever rule it invokes. 
  2052.         So L in rule 1 is the original List2, or [a,b,c].
  2053.  
  2054.              This example would not have worked if the order of rules one 
  2055.         and  two  were  reversed.  The  PROLOG  inference  engine  always 
  2056.         attempts to use the the first rule encountered. You could imagine 
  2057.         it as always reading your program from the top down in attempting 
  2058.         to  find an appropriate rule.  Since rule 2 would always satisfy, 
  2059.         an  unpleasant  thing  would  have happened  if  the  order  were 
  2060.         reversed. The program would loop forever.
  2061.              
  2062.  
  2063.  
  2064.  
  2065.              I  hope  that  this tiny introduction to PROLOG  whets  your 
  2066.         appetite. You should now purchase the book
  2067.  
  2068.              Programming In Prolog
  2069.              W.F. Clocksin and C.S. Mellish
  2070.              Springer - Verlag
  2071.              Berlin,Heidelberg,New York. 1981,1984
  2072.  
  2073.  
  2074.  
  2075.  
  2076.  
  2077.  
  2078.  
  2079.  
  2080.  
  2081.  
  2082.  
  2083.  
  2084.  
  2085.  
  2086.  
  2087.  
  2088.  
  2089.  
  2090.  
  2091.  
  2092.  
  2093.  
  2094.  
  2095.  
  2096.  
  2097.  
  2098.  
  2099.  
  2100.  
  2101.  
  2102.  
  2103.  
  2104.  
  2105.  
  2106.  
  2107.                                        32
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.